<template lang="pug">
div
  CascadeFilter(
      :showFilter="showFilter"
      :filterData="filterData"
      :cascade="cascade"
      @close="showFilter = false"
      @confirm="confirm"
      )

  FixedScroll
    div(slot="fixed")
      PageHeader(pageTitle="盘点详情")
        div(slot="right" @click="showFilter = true") 筛选
  
      g-v.info(v-if="info")
        KvPair(v-if="info.StorgelocationCode" k="实体仓库" :v="info.StorgelocationCode")
        //- KvPair(
            v-if="info.CountTime"
            k="计划盘点时间"
            :v="info.CountTime | Date"
            :spanStyle="{'text-align': 'end'}"
            )
        KvPair(v-if="info.ProductionNo" k="产品线" :v="info.ProductionNo")
        KvPair(v-if="info.Isdps" k="dps" :v="info.Isdps")
        KvPair(
            v-if="info.CountStatus"
            k="状态"
            :v="info.CountStatus | Enum('StockTaking', 'Status')"
            )
      g-h.save(j-c="flex-end")
        Button(
            v-if="!readOnly"
            @click="save"
            text="保存当前页"
            )
  
    StatTable.table(
        slot="scroll"
        :headList="headList"
        :body="body"
        :isLoading="isLoading"
        @check-input="checkInput"
        )

    Pager(
        slot="fixed-bottom"
        :maxIndex="maxIndex"
        :curIndex="curIndex"
        @jump="jump"
        )

</template>

<script>
import api from "$api"
import util from "$util"

const PLANT = "2290"

export default {
  data() {
    // 设置级联
    let cascade = [
      ["WmNo", "ZoneNo"],
    ]

    let StorgelocationCode = this.$route.query.StorgelocationCode

    // 配置筛选
    let filterData = [
      {
        k: "Keeper",
        kTitle: "保管员",
        tag: "input",
        validate: {
          required: true,
        },
      },
      {
        k: "WmNo",
        kTitle: "逻辑仓库",
        tag: "dropdown",
        async getOptionList() {
          try {
            let data = await api.StockFilterGetLogical({
              Plant: PLANT,
              StorgelocationCode,
            })
            let newData = []
            for (let item of data) {
              if (item.ItemValue !== "") {
                newData.push({
                  id: item.ItemValue,
                  name: item.ItemText,
                })
              }
            }
            return newData
          } catch(err) {
            throw err
          }
        },
      },
      {
        k: "ZoneNo",
        kTitle: "存储区",
        tag: "dropdown",
        async getOptionList(req) {
          req.Plant = PLANT
          req.StorgelocationCode = StorgelocationCode
          try {
            let data = await api.StockFilterGetZone(req)
            let newData = []
            for (let item of data) {
              if (item.ItemValue !== "") {
                newData.push({
                  id: item.ItemValue,
                  name: item.ItemText,
                })
              }
            }
            return newData
          } catch(err) {
            throw err
          }
        },
      },
      {
        k: "Dloc",
        kTitle: "库位",
        tag: "input",
        scan: true,
      },
      {
        k: "PartNo",
        kTitle: "物料号",
        tag: "input",
      },
      {
        k: "NotInput",
        kTitle: "未录入",
        tag: "switch",
        switchValue: false,
      },
    ]

    return {
      searchValue: "",
      isLoading: false,
      hasIllegalInput: false,
      readOnly: true,
      showFilter: false,
      filterData,
      cascade,

      loading: {
        save: false,
        confirm: false,
        getInfo: false,
      },

      curIndex: 1,
      maxIndex: 1,
      lastReq: {
        type: "",
        param: null,
      },

      info: null,
      body: {
        list: [],
      },

      headList: [
        { field: "PartNo", title: "物料编号", checked: true },
        { field: "PartCname", title: "物料名称", checked: true },
        { field: "SupplierNum", title: "供应商代码", checked: true },
        { field: "SupplierCode", title: "供应商简称", checked: true },
        { field: "Num", title: "账面数", checked: true },
        { field: "RealNum", title: "实盘数", checked: true },
        { field: "Keeper", title: "保管员", checked: true },
        { field: "Dloc", title: "库位", checked: true },
        { field: "Comments", title: "备注", checked: true },
        { field: "WmNo", title: "逻辑仓库", checked: true },
        { field: "ZoneNo", title: "存储区", checked: true },
      ],
    }
  },

  methods: {
    checkInput(v) {
      if (v == null || v === "") {
        return
      }
      let isNatural = /^(0|[1-9]\d*)$/.test(v)
      if (!isNatural) {
        this.hasIllegalInput = true
        util.Toast(`错误的输入: ${v}`)
      } else if (this.hasIllegalInput) {
        this.hasIllegalInput = false
      }
    },

    async filter(param, pageIndex, pageSize) {
      param.startRowIndex = (pageIndex - 1) * pageSize + 1
      param.maximumRows = pageSize

      this.isLoading = true
      try {
        let data = await api.StockTakingGetDetail(param)
        if (!data) {
          util.Toast("没有找到符合条件的记录")
          return
        }

        if (data.NotificationInfo) {
          switch (data.NotificationInfo.CountStatus) {
            case 1:
            case 2:
              this.readOnly = false
              for (let item of this.headList) {
                if (item.field === "RealNum") {
                  item.isInput = true
                  break
                }
              }
              break
          }
        }

        this.transform(data)
        this.info = data.NotificationInfo
        this.body.list = data.NotificationDetailInfo
        this.maxIndex = Math.ceil(data.total / pageSize)
        this.curIndex = pageIndex
      } catch(err) {
        this.$root.error(err)
      } finally {
        this.isLoading = false
      }
    },

    async getInfo(value, pageIndex = 1, pageSize = 20) {
      if (this.loading.getInfo) {
        return
      }
      if (value === "") {
        return
      }

      let param = {
        NotificationNo: value,
      }

      this.lastReq.type = "getInfo"
      this.lastReq.param = value

      this.loading.getInfo = true
      await this.filter(param, pageIndex, pageSize)
      this.loading.getInfo = false
    },

    async confirm(filter, pageIndex = 1, pageSize = 20) {
      if (this.loading.confirm) {
        return
      }

      let param = { NotificationNo: this.$route.query.NotificationNo }
      for (let item of filter) {
        if (item.value === false) {
          item.value = 1
        } else if (item.value === true) {
          item.value = 0
        }
        param[item.key] = item.value
      }

      this.lastReq.type = "confirm"
      this.lastReq.param = filter

      this.loading.confirm = true
      await this.filter(param, pageIndex, pageSize)
      this.loading.confirm = false
    },

    async save() {
      if (this.loading.save) {
        return
      }
      if (this.body.list.length === 0) {
        return
      }
      if (this.hasIllegalInput) {
        util.Alert("输入框中包含不合法的数据")
        return
      }
      for (let item of this.body.list) {
        if (!item.RealNum) {
          util.Alert("有实盘数为空")
          return
        }
        item.RealNum = parseInt(item.RealNum)
      }

      this.loading.save = true
      try {
        await api.StockTakingSaveDetail({
          Notificationid: this.info.NotificationId,
          NotificationDetailList: this.body.list,
        })
        util.Toast("保存成功")
        // this.$root.Back()
      } catch(err) {
        this.$root.error(err)
      } finally {
        this.isLoading = false
        this.loading.save = false
      }
    },

    transform(data) {
      if (data.NotificationInfo.Isdps == 0) {
        data.NotificationInfo.Isdps = "否"
      } else {
        data.NotificationInfo.Isdps = "是"
      }
    },

    jump(p) {
      if (this.lastReq.type === "confirm") {
        this.confirm(this.lastReq.param, p.curIndex)
      } else {
        this.getInfo(this.lastReq.param, p.curIndex)
      }
    },
  },

  created() {
    this.getInfo(this.$route.query.NotificationNo)
  },
}
</script>

<style lang="stylus" scoped>

.info
  border: 1px solid grey200

.table
  scan-table()

.bottom-bar
  bottom-bar()

.save
  margin: 0 0.5rem 0.2rem 0

</style>
